package com.qyhl.webtv.module_circle.circle.detail;

import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.circle.CircleHomeBean;
import com.qyhl.webtv.commonlib.entity.circle.ShieldResultBean;
import com.qyhl.webtv.module_circle.common.CircleUrl;
import com.qyhl.webtv.module_circle.common.CircleUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by qyhl on 2018/1/29.
 */

public class CircleDetailModel implements CircleDetailContract.CircleDetailModel {
    private CircleDetailPresenter mPresenter;
    String userName;
    public CircleDetailModel(CircleDetailPresenter mPresenter) {
        this.mPresenter = mPresenter;
        userName  = CommonUtils.getInstance().getUserName();
    }

    /**
     * 获取详情
     *
     * @param topicId
     */
    @Override
    public void getDetail(String topicId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=topicInfo" + "&username=" + userName + "&topicId=" + topicId))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<CircleHomeBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setError(1, CircleUtils.getCircleMessage(e.getCode()));
                    }

                    @Override
                    public void onSuccess(CircleHomeBean circleHomeBean) {
                        mPresenter.setDetail(circleHomeBean);
                    }
                });

    }

    /**
     * 获取更多评论
     *
     * @param topicId
     * @param lastId
     */
    @Override
    public void getComment(String topicId, String lastId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=getPostInfo" + "&username=" + userName + "&topicId=" + topicId + "&lastPostId=" + lastId))
                .decryptKet(code)
                .execute(new SimpleCallBack<List<CircleHomeBean.PostList>>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setError(2, CircleUtils.getCircleMessage(e.getCode()));
                    }

                    @Override
                    public void onSuccess(List<CircleHomeBean.PostList> postLists) {
                        mPresenter.setComment(postLists);
                    }
                });


    }

    /**
     * 关注
     *
     * @param dstUserName
     */
    @Override
    public void doFollow(String dstUserName) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=follow" + "&username=" + userName + "&dstUsername=" + dstUserName))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.followError("网络异常，关注失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.followSuccess("关注成功！");
                        } else {
                            mPresenter.followError(CircleUtils.getCircleMessage(apiResult.getCode()));
                        }
                    }
                });
    }

    /**
     * 点赞
     *
     * @param topicId
     */
    @Override
    public void doLove(String topicId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite()+ "&method=loveTopic" + "&username=" + userName + "&topicId=" + topicId))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.doLoveError("网络异常，点赞失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.doLoveSuccess();
                        } else {
                            mPresenter.doLoveError(CircleUtils.getCircleMessage(apiResult.getCode()));
                        }
                    }
                });

    }

    /**
     * 取消点赞
     *
     * @param topicId
     */
    @Override
    public void undoLove(String topicId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=cancleLove" + "&username=" + userName + "&topicId=" + topicId))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.undoLoveError("网络异常，取消失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.undoLoveSuccess();
                        } else {
                            mPresenter.undoLoveError(CircleUtils.getCircleMessage(apiResult.getCode()));
                        }
                    }
                });


    }

    /**
     * 发送评论
     *
     * @param topicId
     * @param content
     * @param dstUsername
     */
    @Override
    public void sendMessage(String topicId, String content, final String dstUsername) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=createPost" + "&username=" + userName + "&topicId=" + topicId
                        + "&content=" + content + "&reply=" + dstUsername))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.commentError("网络异常，评论失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            if (StringUtils.isNotEmpty(dstUsername)) {
                                mPresenter.commentSuccess("评论成功！", 1);
                            } else {
                                mPresenter.commentSuccess("评论成功！", 0);
                            }
                        } else if (apiResult.getCode() == 155) {
                            mPresenter.commentError("您已经被管理员限制言论！");
                        } else {
                            mPresenter.commentError(CircleUtils.getCircleMessage(apiResult.getCode()));
                        }
                    }
                });

    }

    @Override
    public void deleteComment(String postId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=deletePost" + "&username=" + userName + "&postId=" + postId))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.deleteError("网络异常，删除失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.deleteSuccess();
                        } else {
                            mPresenter.deleteError("评论或已删除，请刷新后重试！");
                        }
                    }
                });

    }

    /**
     * 删除主题
     *
     * @param topic
     */
    @Override
    public void deleteTopic(String topic) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=deleteTopic" + "&username=" + userName + "&topicId=" + topic))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.deleteTopicError("网络异常，删除失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.deleteTopicSuccess();
                        } else {
                            mPresenter.deleteTopicError("该主题或已删除，删除失败！");
                        }
                    }
                });

    }

    @Override
    public void checkShield(String dstUsername) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=mailPermit" + "&username=" + userName + "&dstUsername=" + dstUsername
                ))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<ShieldResultBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.checkResult(0, CircleUtils.getCircleMessage(e.getCode()));
                    }

                    @Override
                    public void onSuccess(ShieldResultBean shieldResultBean) {
                        if (shieldResultBean.isDefriend()) {
                            mPresenter.checkResult(0, "对不起，对方拒绝和你聊天！");
                        } else if (shieldResultBean.isGag()) {
                            mPresenter.checkResult(0, "对不起，管理员禁言了你！");
                        } else {
                            mPresenter.checkResult(1, "success");
                        }
                    }
                });

    }

}
